package com.tanhua.dubbo.api;

import com.tanhua.api.UserLikeApi;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    //保存用户左滑喜欢用户数据
    @Override
    public boolean save(Long getid, Long likeId, boolean b) {
        try {
            Query query = new Query(Criteria.where("userId").is(getid).and("likeUserId").is(likeId));
            //判断佳人是否在用户喜欢列表
            boolean exists = mongoTemplate.exists(query, UserLike.class);
            //如果存在就修改
            if (exists){
                Update update = new Update();
                update.set("isLike",b).set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }else {
                UserLike userLike = new UserLike();
                userLike.setLikeUserId(likeId);
                userLike.setUserId(getid);
                userLike.setIsLike(b);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike);
            }
            return true;

        }catch (Exception e) {
            return false;
        }



    }
//根据用户id和喜欢的用户查询信息
    @Override
    public UserLike findById(Long id, Long getid,boolean isLike) {
        Query query = new Query(Criteria.where("userId").is(id).and("likeUserId").is(getid).and("isLike").is(isLike));
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        return userLike;
    }
}
